package com.ujiuye.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ujiuye.dao.ICategoryMapper;
import com.ujiuye.domain.dto.PageDTO;
import com.ujiuye.domain.dto.SaveCategoryDTO;
import com.ujiuye.domain.po.Category;
import com.ujiuye.domain.vo.CategoryListVO;
import com.ujiuye.domain.vo.PageVO;
import com.ujiuye.exceptions.MyException;
import com.ujiuye.service.ICategoryService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private ICategoryMapper categoryMapper;

    @Override
    public List<CategoryListVO> findALl() {
        List<Category> categoryList = categoryMapper.findAll();
        List<CategoryListVO> collect = categoryList.stream().map(category -> {
            CategoryListVO categoryListVO = new CategoryListVO();
            BeanUtils.copyProperties(category, categoryListVO);
            return categoryListVO;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public CategoryListVO getById(int id) {
        Category category = categoryMapper.getById(id);
        CategoryListVO categoryListVO = new CategoryListVO();
        BeanUtils.copyProperties(category, categoryListVO);
        return categoryListVO;
    }

    @Override
    public void saveCategory(SaveCategoryDTO saveCategoryDTO) {
        if (saveCategoryDTO.getId() == 0) {
            categoryMapper.add(saveCategoryDTO);
        } else {
            Category byId = categoryMapper.getById(saveCategoryDTO.getId());
            if (byId == null) {
                MyException.error("更新失败,未查询到该分类");
            }
            categoryMapper.update(saveCategoryDTO);
        }
    }

    @Override
    public void deleteById(int id) {
        Category byId = categoryMapper.getById(id);
        if (byId == null) {
            MyException.error("未查询到该分类");
        }
        categoryMapper.deleteById(id);
    }

    @Override
    public PageVO<CategoryListVO> page(PageDTO<String> pageDTO) {
        log.info("pageDto{}",pageDTO);
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getPageSize());
        List<Category> categoryList = categoryMapper.pageQuery(pageDTO.getQueryParam());

        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        log.info("pageInfo{}",pageInfo.getList());

        List<CategoryListVO> collect = pageInfo.getList().stream().map(category -> {
            CategoryListVO categoryListVO = new CategoryListVO();
            BeanUtils.copyProperties(category, categoryListVO);
            return categoryListVO;
        }).collect(Collectors.toList());

        //拼装分页VO
        PageVO<CategoryListVO> pageVO = new PageVO<>();
        pageVO.setPage(pageDTO.getPage());
        pageVO.setPageSize(pageDTO.getPageSize());
        pageVO.setTotal(pageInfo.getTotal());
        pageVO.setRecords(collect);

        return pageVO;
    }
}
